﻿use MusicmanStaging
go 

-- procedure update [UpdateUserStats]
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[UpdateUserStats]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[UpdateUserStats]
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[UpdateUserStats]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE [dbo].[UpdateUserStats]
	@PersonId bigint,
	@web int,
	@client int

AS
BEGIN
 IF NOT EXISTS(SELECT * FROM DBO.USERSTATS WHERE PersonID = @PersonId)
 BEGIN
	INSERT INTO DBO.USERSTATS(PERSONID, NUMLOGSWEB, NUMLOGSCLIENT) VALUES (@PersonId, @web, @client);
 END 
 ELSE 
 BEGIN 
    UPDATE DBO.USERSTATS SET NUMLOGSWEB = NUMLOGSWEB + @web, NUMLOGSCLIENT = NUMLOGSCLIENT + @client WHERE PERSONID = @PersonId
 END
END
' 

END
GO
